import { createRouter, createWebHistory } from "vue-router";
import routes from "./static.js";

const router = createRouter({
  history: createWebHistory(),
  routes,
});

router.beforeEach((to, from, next) => {
  const token = localStorage.getItem("token");
  const whiteList = ["/login", "/404"];

  if (token) {
    if (to.path === "/login") {
      message.warning("您已登录，无需重复登录");
      next("/");
    } else {
      next();
    }
  } else {
    if (whiteList.includes(to.path)) {
      next();
    } else {
      message.info("请先登录后再访问");
      next("/login");
    }
  }
});

router.afterEach(() => {});

export default router;
